//
//  ViewController.swift
//  SelfTodo
//
//  Created by panda on 17/3/17.
//  Copyright © 2017年 panda. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    var editBtn = UIButton()
    var isEdit: Bool = false {
        willSet(value) {
            if value {
                tableV.setEditing(value, animated: true)
                editBtn.setTitle("Done", for: UIControlState.normal)
            }else {
                tableV.setEditing(value, animated: true)
                editBtn.setTitle("edit", for: UIControlState.normal)
            }
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        title = "ToDo List"
        
        isEdit = false
        
        tableV.frame = CGRect(x: 0, y: 0, width: 375, height: 400)
        
        view.addSubview(tableV)
        
        editBtn = UIButton(type: UIButtonType.custom)
        editBtn.frame = CGRect(x: 0, y: 0, width: 40, height: 30)
        editBtn.setTitleColor(UIColor.blue, for: .normal)
        editBtn.setTitle("edit", for: UIControlState.normal)
        editBtn.addTarget(self, action: #selector(editBtnAction), for: UIControlEvents.touchUpInside)
        let barItem = UIBarButtonItem(customView: editBtn)
        navigationItem.leftBarButtonItem = barItem
    }
    
    func editBtnAction() {
        isEdit = !isEdit
    }
    
    //MARK: - 懒加载
    fileprivate lazy var tableV : UITableView = {
       let table = UITableView()
//        table.separatorStyle = .none
        table.dataSource = self
        table.delegate = self
        return table
    }()

}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellId = "cell"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellId)
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: cellId)
        }
        cell?.textLabel?.text = "12345"
        return cell!
    }
    
    //是否允许打开编辑状态
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    //允许滑动删除
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle.delete
    }
    
    //允许移动某行（排序）
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    //
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        tableView.moveRow(at: sourceIndexPath, to: destinationIndexPath)
    }
}

